package com.fin.zw.aiqas.service;

import com.fin.zw.aiqas.mapper.RuleStatisticsMapper;
import com.fin.zw.aiqas.model.component.DynamicHeadModel;
import com.fin.zw.aiqas.model.component.ServiceDynamicModel;
import com.fin.zw.aiqas.model.component.ServiceLeftModel;
import com.fin.zw.aiqas.model.data.StatisticsDetailModel;
import com.fin.zw.aiqas.model.request.RuleStatisticsRequest;
import com.fin.zw.aiqas.model.request.StatisToServiceRequest;
import com.fin.zw.aiqas.model.response.QualityInfo;
import com.fin.zw.aiqas.model.response.RuleStatisticsResponse;
import com.fin.zw.aiqas.model.response.StatisticsToServiceResponse;
import com.fin.zw.aiqas.repository.RuleInfoRepository;
import com.fin.zw.aiqas.util.DateUtil;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.ListUtils;

import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

@Service("ruleStatisticsService")
public class RuleStatisticsService {

    @Autowired(required = true)
    RuleStatisticsMapper ruleStatisticsMapper;
    @Autowired
    private RuleInfoRepository ruleInfoRepository;

    public List<RuleStatisticsResponse> queryRuleStatistics(RuleStatisticsRequest request) {
        return ruleStatisticsMapper.selectRuleStatistics(request);
    }
    public Integer selectRuleStatisticsCount(RuleStatisticsRequest request) {
        return ruleStatisticsMapper.selectRuleStatisticsCount(request);
    }

    /**
     * 客服统计
     *
     * @param request
     * @return
     */
    public StatisticsToServiceResponse queryCustomerServer(StatisToServiceRequest request) {
        List<ServiceLeftModel> serviceLeftModels = queryCustomerServerLeft(request);
        List<ServiceDynamicModel> serviceDynamicModels = queryServiceDynamic(request);
        /** 表头 */
        LinkedHashSet<DynamicHeadModel> headSet = new LinkedHashSet<>();
        headSet.add(new DynamicHeadModel("deptName", "部门"));
        headSet.add(new DynamicHeadModel("serviceCode", "客服编号"));
        headSet.add(new DynamicHeadModel("checkProp", "抽检文件数"));
        headSet.add(new DynamicHeadModel("hitNum", "命中文件数"));
        headSet.add(new DynamicHeadModel("hitRate", "文件命中率"));
        headSet.add(new DynamicHeadModel("hitRuleCount", "规则命中数"));
        for (ServiceDynamicModel serviceDynamicModel : serviceDynamicModels) {
            headSet.add(new DynamicHeadModel(serviceDynamicModel.getRuleId(), serviceDynamicModel.getRuleName()));
        }
        List<DynamicHeadModel> headList = Lists.newArrayList(headSet);

        /** 数据 */
        List<LinkedHashMap<String, String>> data = new ArrayList<>();
        serviceLeftModels.forEach(left -> {
            // 一行的数据
            LinkedHashMap<String, String> mapData = new LinkedHashMap<>();
            //没有数据的就显示 0
            headList.forEach(dynamicHeadModel -> {
                mapData.put(dynamicHeadModel.getId(), "0");
            });
            mapData.put("deptName", left.getDeptName());
            mapData.put("serviceCode", left.getServiceCode());
            mapData.put("checkProp", left.getCheckProp());
            mapData.put("hitNum", left.getHitNum());
            mapData.put("hitRate", left.getHitRate());
            //一行数据中右边的动态数据
            serviceDynamicModels.stream()
                    .filter(obj -> obj.getServiceCode().equals(left.getServiceCode()))
                    .collect(Collectors.toList()).forEach(serviceCode -> {
                mapData.put("hitRuleCount", serviceCode.getCountHitRule());
                mapData.put(serviceCode.getRuleId(), serviceCode.getRate());
            });
            data.add(mapData);
        });
        StatisticsToServiceResponse responses = new StatisticsToServiceResponse();
        responses.setHead(headList);
        responses.setData(data);
        return responses;
    }

    /**
     * 客服统计左边数据
     *
     * @param request
     * @return
     */
    public List<ServiceLeftModel> queryCustomerServerLeft(StatisToServiceRequest request) {
        return ruleStatisticsMapper.selectCustomerServer(request);
    }

    /**
     * 客服统计查询右边动态数据
     *
     * @return
     */
    public List<ServiceDynamicModel> queryServiceDynamic(StatisToServiceRequest request) {
        return ruleStatisticsMapper.queryServiceDynamic(request);
    }


    public QualityInfo qualityInfo() {
        QualityInfo qualityInfo = new QualityInfo();
        Integer qualityNumber = ruleStatisticsMapper.selectCountInspectionDetailByDate();
        Integer ruleNumber = ruleStatisticsMapper.selectCountRuleByDate();
        qualityInfo.setQualityNumber(qualityNumber);
        qualityInfo.setRuleNumber(ruleNumber);
        return qualityInfo;
    }


    public List<StatisticsDetailModel> statisticsRecord() {
        Date start = DateUtil.getBeforeMonth();
        List<StatisticsDetailModel> list = ruleStatisticsMapper.selectDetailByDate(start, new Date());
        Long days = DateUtil.minusLocalDate(start, new Date());
        for (int i = 0; i < days + 1; i++) {
            LocalDate localDate = DateUtil.addDays(start, i);
            if (!exitStatisticsDetailModelByDate(list, localDate.toString())) {
                StatisticsDetailModel model = new StatisticsDetailModel();
                model.setOperateDate(localDate.toString());
                model.setNumber(0);
                list.add(model);
            }
        }
        List<StatisticsDetailModel> result = list.stream().sorted((u1, u2) -> u1.getOperateDate().compareTo(u2.getOperateDate())).collect(Collectors.toList());
        return result;
    }

    public boolean exitStatisticsDetailModelByDate(List<StatisticsDetailModel> list, String date) {
        boolean result = false;
        if (!ListUtils.isEmpty(list)) {
            for (StatisticsDetailModel model : list) {
                if (model.getOperateDate().equals(date)) {
                    result = true;
                    break;
                }
            }
        }
        return result;
    }

    /**
     * 统计最近一个月规则出现录音的数量
     *
     * @return
     */
    public List<StatisticsDetailModel> selectInspectionByRule() {
        Date start = DateUtil.getBeforeMonth();
        List<StatisticsDetailModel> list = ruleStatisticsMapper.selectInspectionByRule(start, new Date());
        return list;
    }

}
